//
//  InStationAlertController.swift
//  iPhone
//
//  Created by Toj on 3/28/23.
//

import UIKit

class InStationAlertController: STBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(button)
        button.frame = CGRect(x: 0, y: 100, width: 120, height: 44)
        
        view.addSubview(pushButton)
        pushButton.frame = CGRect(x: 0, y: 188, width: 120, height: 44)
    }
    
    private lazy var button: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("站内弹窗开始", for: .normal)
        view.addTarget(self, action: #selector(didTapShowAlert), for: .touchUpInside)
        return view
    }()
    
    private lazy var pushButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("跳走", for: .normal)
        view.addTarget(self, action: #selector(didTapPush), for: .touchUpInside)
        return view
    }()
    
    private func alertViews() {
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 6) {
            self.alertMore1()
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.alertMore2()
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.alertMore3()
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            self.alertMore4()
        }
    }
}

private extension InStationAlertController {
    
    @objc func didTapPush() {
        let vc = STLineViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func didTapShowAlert() {
        if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
            
            let vc = InStationAlertController()
            vc.view.frame = CGRect(x: 0, y: 0, width: .sr_width, height: .sr_height)
            vc.view.backgroundColor = UIColor.black.tt_alpha(0.5)
            vc.willMove(toParent: rootVC)
            vc.willMove(toParent: rootVC)
            rootVC.addChild(vc)
            rootVC.view.addSubview(vc.view)
            vc.didMove(toParent: rootVC)
        }
    }
    
    func alertMore1() {
        var model = STAlertModel()
        model.title = "Alert 1"
        model.popDescription = "桌上的坚果开心果5"
        model.topPicUrl = "http://vip.meisupic.com/goods/goods.php?a=1156252"
        model.popUrl = "http://vip.meisupic.com/goods/goods.php?a=1156282"
        STAlertHelper.manager.show(alerModel: model) {
            let vc = STSettingController()
            UIViewController.vc_currentNavigation?.pushViewController(vc, animated: true)
            // print("-->toj: default1 action")
        } cancelAction: {
            // print("-->toj: cancel1 action")
        } effectAction: {
            // print("-->toj: effect1 action")
        }
    }
    
    func alertMore2() {
        var model = STAlertModel()
        model.title = "Alert 2"
        model.popDescription = "盘中的香酥椒1"
        model.topPicUrl = "http://vip.meisupic.com/goods/goods.php?a=1156273"
        model.popUrl = "http://vip.meisupic.com/goods/goods.php?a=1156263"
        STAlertHelper.manager.show(alerModel: model) {
            let vc = TVLinkingCardViewController()
            UIViewController.vc_currentNavigation?.pushViewController(vc, animated: true)
            // print("-->toj: default2 action")
        } cancelAction: {
            // print("-->toj: cancel2 action")
        } effectAction: {
            // print("-->toj: effect2 action")
        }
    }
    
    func alertMore3() {
        var model = STAlertModel()
        model.title = "Alert 3"
        model.topPicUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi.qqkou.com%2Fi%2F0a3555809371x1438535940b26.jpg&refer=http%3A%2F%2Fi.qqkou.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665802388&t=baa31eb1d81af34f3e884e8a590efe04"
        model.popUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.duoziwang.com%2F2017%2F04%2F29%2FB7936.jpg&refer=http%3A%2F%2Fimg.duoziwang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665802396&t=3457aa185045c0c5ceabe1f767937b3e"
        let delegate = UIApplication.shared.delegate as? AppDelegate
        let rootVC = delegate?.window?.rootViewController as? UITabBarController
        STAlertHelper.manager.show(alerModel: model) {
            UIViewController.vc_currentNavigation?.popToRootViewController(animated: false)
            rootVC?.selectedIndex = 3
            // print("-->toj: default3 action")
        } cancelAction: {
            // print("-->toj: cancel3 action")
        } effectAction: {
            // print("-->toj: effect3 action")
        }
    }
    
    func alertMore4() {
        var model = STAlertModel()
        model.title = "Alert 4"
        model.popDescription = "拔丝山楂的做法材料:新鲜山楂12个、白砂糖120克、纯净水35克步骤1、将白砂糖倒入不粘小奶锅中称好。2、水称好3、山楂去蒂洗净用厨房纸搽干水份备用。"
        model.topPicUrl = "http://www.4493.com/meishi/50956/1.htm"
        model.popUrl = "http://www.4493.com/meishi/30508/1.htm"
        STAlertHelper.manager.show(alerModel: model) {
            // print("-->toj: default4 action")
        } cancelAction: {
            // print("-->toj: cancel4 action")
        } effectAction: {
            // print("-->toj: effect4 action")
        }
    }
}
